package cn.zhentao.dao.impl;

import cn.zhentao.dao.TeacherDao;
import cn.zhentao.pojo.Teacher;
import cn.zhentao.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class TeacherDaoImpl implements TeacherDao {

    //查询所有数据方法
    public List<Teacher> findAll() throws SQLException {
        Connection conn = DBUtil.getConn();
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(FIND_ALL);
        List<Teacher> list = new ArrayList<>();
        while (rs.next()) {
            int tid = rs.getInt("tid");
            String tname = rs.getString("tname");
            String password = rs.getString("password");
            Date hiredate = rs.getDate("hiredate");
            String tel = rs.getString("tel");
            String address = rs.getString("address");
            Teacher t = new Teacher(tid, tname, password, hiredate, tel, address);
            list.add(t);
        }
        //返回list集合
        return list;
    }

    //模拟登录方法
    public Teacher login(String tname, String password) throws Exception {
        //声明SQL语句
        //？：占位符
        String sql = "select * from teacher where tname=? and password=?";
        System.out.println("执行SQL："+sql);
        Connection conn = DBUtil.getConn();
        //声明预编译对象，加载SQL语句
        PreparedStatement prep = conn.prepareStatement(sql);
        // 调用方法，将？替换为 参数的值
        // 方法参数 （?的序号，参数值）
        prep.setString(1,tname);
        prep.setString(2,password);

        //执行SQL语句，获取结果集
        ResultSet rs = prep.executeQuery();
        //声明teacher类对象
        Teacher t = null;
        while (rs.next()) {
            int tid = rs.getInt("tid");
            String tname1 = rs.getString("tname");
            String password1 = rs.getString("password");
            Date hiredate = rs.getDate("hiredate");
            String tel = rs.getString("tel");
            String address = rs.getString("address");
            //将获取的数据，存入到t中
            t = new Teacher(tid,tname1,password1,hiredate,tel,address);
        }
        return t;
    }

    //删除数据
    @Override
    public int delTeacher(int tid) throws SQLException {
        Connection conn = DBUtil.getConn();
        PreparedStatement prep = conn.prepareStatement(DELETE);
        //设置参数
        prep.setInt(1,tid);
        int i = prep.executeUpdate();
        return i;
    }

    @Override
    public int addTeacher(Teacher t) throws SQLException {
        Connection conn = DBUtil.getConn();
        PreparedStatement prep = conn.prepareStatement(INSERT);
        prep.setString(1,t.getTname());
        prep.setString(2,t.getPassword());
        prep.setDate(3,t.getHiredate());
        prep.setString(4,t.getTel());
        prep.setString(5,t.getAddress());

        int i = prep.executeUpdate();

        return i;
    }

    @Override
    public int updateTeacher(Teacher t) throws SQLException {
        Connection conn = DBUtil.getConn();
        PreparedStatement prep = conn.prepareStatement(UPDATE);

        prep.setString(1,t.getTname());
        prep.setString(2,t.getPassword());
        prep.setDate(3,t.getHiredate());
        prep.setString(4,t.getTel());
        prep.setString(5,t.getAddress());
        prep.setInt(6,t.getTid());

        int i = prep.executeUpdate();

        return i;
    }


}